package com.lee.shiro.util;

import org.apache.shiro.crypto.hash.SimpleHash;

import java.util.UUID;

public class MD5EncryptionUtil {

    /**
     *  根据用户名随机生成盐值
     * @param username 用户名
     * @return
     */
    public static String getSalt(String username) {
        String salt = UUID.nameUUIDFromBytes(username.getBytes()).toString().replaceAll("-", "");
        return salt;
    }

    /**
     * 对原始密码进行加密
     * @param originalPassword
     * @param salt
     * @return
     */
    public static String encryptionPwd(String originalPassword, String salt, String hashAlgorithm, int hashIteration) {
        SimpleHash newPassword = new SimpleHash(hashAlgorithm,originalPassword,salt,hashIteration);
        return newPassword.toString();
    }

    public static void main(String[] args) {
        String username = "zhangsan";
        String password = "123456";
        String hashAlgorithm = "md5";
        String salt = getSalt(username);
        int hashIteration = 2;

        System.out.println(encryptionPwd(password, salt, hashAlgorithm, hashIteration));
    }
}
